//
//  DetaillsTableViewController.m
//  Otaku
//
//  Created by lanou3g on 16/5/9.
//  Copyright © 2016年 Liuwx. All rights reserved.
//

#import "DetaillsTableViewController.h"
#import "DetailsModel.h"
#import "JokeCell.h"
#import "DetailsCell.h"

@interface DetaillsTableViewController ()

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) CGFloat hight;
//请求内容起始条数
@property (nonatomic, strong) NSString *start;

@property (nonatomic, strong) NSString *currentSkinModel;
@end

@implementation DetaillsTableViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = [NSMutableArray new];
    self.navigationItem.title = @"评论";
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self loadDataFromServerType:self.type Start:self.start];
    
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"JokeCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DetailsCell" bundle:nil] forCellReuseIdentifier:@"detacell"];
    
    //添加上提加载更多
    __weak typeof(self) weakSelf = self;
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        NSInteger count = weakSelf.dataArray.count;
        weakSelf.start = [NSString stringWithFormat:@"%ld",count];
        [weakSelf loadDataFromServerType:weakSelf.type Start:weakSelf.start];
    }];
}

- (void)dealloc {
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentOffset"];
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentSize"];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateSkinModel) name:SkinModelDidChangedNotification object:nil];
    [self updateSkinModel];
    
}


- (void)updateSkinModel {
    
    self.currentSkinModel = [[NSUserDefaults standardUserDefaults] stringForKey:CurrentSkinModelKey];
    
    if ([self.currentSkinModel isEqualToString:NightSkinModelValue]) {
        self.view.backgroundColor = [UIColor blackColor];
        //        self.nameTextField.backgroundColor = [UIColor darkGrayColor];
        //        self.SignatureTextView.backgroundColor = [UIColor darkGrayColor];
        //        self.nameTextField.textColor  = [UIColor grayColor];
        //        self.SignatureTextView.textColor = [UIColor grayColor];
        
    }else {
        self.view.backgroundColor = [UIColor colorWithRed:250.0/255.0 green:250.0/255.0  blue:250.0/255.0  alpha:1.0];
        //        self.nameTextField.backgroundColor = [UIColor whiteColor];
        //        self.SignatureTextView.backgroundColor = [UIColor whiteColor];
        //        self.nameTextField.textColor = [UIColor blackColor];
        //        self.SignatureTextView.textColor = [UIColor blackColor];
        
    }

}

//解析数据
-(void)loadDataFromServerType:(NSString *)type Start:(NSString *)start {
    
    [LWXNetWorkRequestManager requestURL:[NSURL URLWithString:@"http://isub.snssdk.com/neihan/comments/"] Method:LWX_GET Params:@{@"group_id":self.jokemodel.ID ,@"item_id":@6431281251,@"count":@20,@"offset":@0,@"iid":@4240136140,@"device_id":@15036532241,@"ac":@"wifi",@"channel":@"ucad",@"aid":@7,@"app_name":@"joke_essay",@"version_code":@500,@"version_name":@"5.0.0",@"device_platform":@"android",@"ssmix":@"a",@"device_type":@"HUAWEI+B199",@"os_api":@18,@"os_version":@"4.3",@"uuid":@"A000004F036FAC",@"openudid":@"e691cf971858edda",@"manifest_version_code":@500} Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            for (NSDictionary *dict in jsonDict[@"data"][@"recent_comments"]) {
                DetailsModel *detaModel = [DetailsModel new];
                [detaModel setValuesForKeysWithDictionary:dict];
                [self.dataArray addObject:detaModel];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
                [[self.tableView defaultFooter]endRefreshing];
            });
        }
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        
        
       JokeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath] ;
        if ([self.currentSkinModel isEqualToString:DaySkinModelValue]) {//日间模式
            [cell updateToDaySkinMode];
        } else {
            [cell updateToNightSkinMode];
        }

        cell.model = self.array[self.indexPath];
        CGFloat highText = [Tool labelHeightWithText:cell.model.text font:[UIFont systemFontOfSize:17]];
        self.hight = highText;
        return cell;
        
    }else {
        
        DetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detacell" forIndexPath:indexPath];
        if ([self.currentSkinModel isEqualToString:DaySkinModelValue]) {//日间模式
            [cell updateToDaySkinMode];
        } else {
            [cell updateToNightSkinMode];
        }

        cell.model = self.dataArray[indexPath.row - 1];
        return cell;
}

    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0) {
        
        return self.hight + 180;
    }else{
        DetailsModel *model = self.dataArray[indexPath.row];
        CGFloat labeleHeight = [Tool labelHeightWithText:model.text font:[UIFont systemFontOfSize:17]];
    
        return labeleHeight + 120;
    }
    
}




/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
